Repository navigation
ADDING mocking script for pesterV5 DOCS - #370
CN-CODEGOD wants to merge 4 commits into
Conversation
Add code block formatting to the mocking documentation.
| ### Mocking function in scriptblock | ||
| Unfortunately, we don't have a straightforward way to mock a function inside a scriptblock, but we could use some workarounds to accomplish this. | ||
|
|
||
| warning:Mocking may not work correctly in Pester versions below v5, so this function is only supported in Pester v5. In earlier versions, you cannot run the mock together with the script. |
There was a problem hiding this comment.
this does not feel right, I wrote about how to do this long time ago, and I don't think there is anything in Pester 5 that would make this work, it is simple mocking.
https://jakubjares.com/2019/06/09/testing-whole-scripts/ https://jakubjares.com/2019/06/14/testing-whole-scripts-part2/
There was a problem hiding this comment.
is there any problem with the script tester ?
As far as i concern the inscript work well
Add-Type -TypeDefinition @'
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Reflection;
namespace ScriptMocking
{
[Cmdlet(VerbsData.Import, "Script")]
public sealed class ImportScriptCommand : PSCmdlet
{
private static string Body = @"
function ____placeholder {{ }}
# alias to prevent main from running
Set-Alias {0} '____placeholder'
. {1} {2}
# remove the alias, now we have all functions
# from the script dot-sourced in this scope
Remove-Item 'alias:{0}' -Force
Remove-Item 'function:____placeholder' -Force";
[Parameter(Position = 0, Mandatory = true)]
public string Path { get; set; }
[Parameter(Position = 1)]
public string EntryPoint { get; set; }
[Parameter]
public string[] ArgumentList { get; set; }
public ImportScriptCommand() {
ArgumentList = new string[0];
EntryPoint = "Main";
}
protected override void ProcessRecord()
{
var body = string.Format(Body, EntryPoint, Path, string.Join(" ", ArgumentList));
var sb = InvokeCommand.NewScriptBlock(body);
var method = sb.GetType()
.GetMethod("InvokeUsingCmdlet", BindingFlags.Instance | BindingFlags.NonPublic);
var emptyArray = new object[0];
var automationNull = new PSObject();
const int writeToCurrentErrorPipe = 1;
var @params = new object[]
{this, false, writeToCurrentErrorPipe, automationNull, emptyArray, automationNull, new object[0]};
method.Invoke(sb, @params);
}
}
}
|
Thanks for writing this up, but I can't take it as it is. The code fence in your section is never closed, and the closing ``` at the end of the file is removed. Everything below your section renders as one code block and the rest of the page is broken. The content is a copy of the workaround from pester/Pester#1069, including my The warning also uses a full width colon instead of a Closing this. The workaround stays in pester/Pester#1069 for anyone who needs it. If you want to document the general problem of testing a script that runs its own entry point, open an issue first and we can agree on the approach. Thanks! 🤖 |
|
i read about the issue
And test the function,it work fine in pesterV5 but fail in V4,it is a good assertion ,hoping to add the in scriptscope in modules future